package servlet;

import base.BaseServlet;
import base.Result;
import biz.UserService;
import pojo.User;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@WebServlet(name = "UserServlet", urlPatterns = "/user")
public class UserServlet extends BaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("+++++dopost+++++++++");
        super.doPost(req, resp);
        User user;
        if (param == null) return;
        try (PrintWriter writer = resp.getWriter()) {
            switch (param) {
                case "admin":
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    user = new User();
                    user.setUsername(jsonRequest.get("username").getAsString());
                    user.setPassword(jsonRequest.get("password").getAsString());
                    writer.write(bean2Json(UserService.start().adminSignIn(user)));
                    break;
                case "signIn":
                    Thread.sleep(2000);
                    user = new User();
                    user.setUsername(jsonRequest.get("username").getAsString());
                    user.setPassword(jsonRequest.get("password").getAsString());
                    writer.write(bean2Json(UserService.start().signIn(user)));
                    break;
                case "signUp":
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    user = new User();
                    user.setUsername(jsonRequest.get("username") != null ? jsonRequest.get("username").getAsString() : null);
                    user.setPassword(jsonRequest.get("password") != null ? jsonRequest.get("password").getAsString() : null);
                    user.setNickname(jsonRequest.get("nickname") != null ? jsonRequest.get("nickname").getAsString() : "未设置用户名");
                    user.setAge(jsonRequest.get("age") != null ? jsonRequest.get("age").getAsInt() : 0);
                    user.setSex(jsonRequest.get("sex") != null ? jsonRequest.get("sex").getAsInt() : 0);
                    user.setUserIdentity(jsonRequest.get("type") != null ? jsonRequest.get("type").getAsInt() : 0);
                    writer.write(bean2Json(UserService.start().signUp(user)));
                    break;
                case "userList"://获取用户列表
                    writer.write(bean2Json(UserService.start().getUserList(jsonRequest.get("page") != null ? jsonRequest.get("page").getAsInt() : null)));
                    break;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            try (PrintWriter writer = resp.getWriter()) {
                System.out.println(ex.getMessage());
                Result<String> result = new Result<>();
                result.setCode(-1);
                result.setMessage("请求太频繁，请稍后重试！");
                result.setData("请求太频繁，请稍后重试！");
                writer.write(bean2Json(result));
            } catch (IOException ex1) {
                ex1.printStackTrace();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroy() {
        super.destroy();
        UserService.start().closeSession();
    }
}
